package com.atguigu.gulimall.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.order.dao.OrderItemDao;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.service.OrderItemService;


@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    //@RabbitListener 可标记在类+方法 指定监听哪些队列
    //@RabbitHandler 可标记在方法上，重载方式区分不同的消息
    //只有在容器中，@RabbitListener注解才有作用
    //Message：原生消息详细信息。头+体
    //会自动转为：T<发送消息的类型> OrderReturnReasonEntity
    //Channel:当前传输数据的通道
    //Queue:可以很多人监听，只要收到消息，队列删除消息，而且只能有一个收到消息，如果接收消息后的 业务处理时间较长，只有等处理完成才能接收下一个消息
    @RabbitListener(queues = {"hello-java-queue"})
    public void recieveMessage(Message message, Channel channel){
        byte[] body = message.getBody();
        System.out.println("接收到消息：："+message+" === 内容："+ new String(body));
        //消息头属性信息
        MessageProperties messageProperties = message.getMessageProperties();
        //deliveryTag :消息的标签,在通道内按顺序自增
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //todo 待处理：签收多个消息期间宕机会导致所有消息都被签收
            if (true) {
                //手动签收(参数：消息标签，是否批量签收)
                channel.basicAck(deliveryTag, false);
            } else {
                //手动拒签（参数：消息标签，是否批量，是否重新入队，不入队则丢弃消息）
                channel.basicNack(deliveryTag,false,true);//支持批量拒签
//                channel.basicReject(deliveryTag,true);//不支持批量拒签
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RabbitHandler
    public void recieveMessage(OrderReturnReasonEntity content){
        System.out.println("接收到消息内容："+content);
    }
    @RabbitHandler
    public void recieveMessage(OrderEntity content){
        System.out.println("接收到消息内容："+content);
    }
}